package ecs

import (
	"tushare/model/mysql"
)

type AliyunEcs struct {
	Id           int64  `json:"id"`
	UserId       int64  `json:"user_id"`
	Ip           string `json:"ip"`
	InstanceId   string `json:"instance_id"`
	RegionId     string `json:"region_id"`
	ZoneId       string `json:"zone_id"`
	InstanceType string `json:"instance_type"`
	ImageId      string `json:"image_id"`
	InstanceName string `json:"instance_name"`
	Status       int64  `json:"status"`
	TaskRemark   string `json:"task_remark"`
	TaskType     string `json:"task_type"`
	CreateTime   string `json:"create_time"`
	IsDelete     int64  `json:"is_delete"`
	InstallApp   string `json:"install_app"`
}

func AliyunEcsCreate(ae AliyunEcs) {
	mysql.DB.Table("aliyun_ecs").Create(&ae)
}

func AliyunEcsList(page, pageSize int64, userId int64) []AliyunEcs {
	page = page
	pageSize = pageSize
	offset := (page - 1) * pageSize

	bf := []AliyunEcs{}
	mysql.DB.Table("aliyun_ecs").Where("is_delete = 0 and user_id = ?", userId).Limit(pageSize).Offset(offset).Scan(&bf)
	return bf
}

func AliyunEcsCount(userId int64) int64 {
	var count int64
	mysql.DB.Table("aliyun_ecs").Where("is_delete = 0 and user_id = ?", userId).Count(&count)
	return count
}

func AliyunEcsListByStatus(status int64) []AliyunEcs {
	bf := []AliyunEcs{}
	mysql.DB.Table("aliyun_ecs").Where("status = ?", status).Scan(&bf)
	return bf
}

func AliyunEcsUpdate(id int64, ae AliyunEcs) {
	mysql.DB.Table("aliyun_ecs").Where("id = ?", id).Update(&ae)
}

func AliyunEcsListAll() []AliyunEcs {
	bf := []AliyunEcs{}
	mysql.DB.Table("aliyun_ecs").Where("is_delete = 0").Scan(&bf)
	return bf
}

func AliyunEcsByUserIdAndInstanceId(userId int64, instanceId string) AliyunEcs {
	ae := AliyunEcs{}
	mysql.DB.Table("aliyun_ecs").Where("user_id = ? and instance_id = ?", userId, instanceId).Scan(&ae)
	return ae
}
